<template>
  <div class="login-box">
    <div class="login-box-logo">
      <!-- 头像 -->
      <img src="../assets/logo.png" alt="" />
    </div>
    <!-- 表单 -->
    <el-form class="login-form" :model="loginform" :rules="loginRules" 
    ref="loginRef" >
      <!-- 用户名 -->
      <el-form-item prop="username">
        <el-input
          type="text"
          placeholder="请输入账号"
          prefix-icon="iconfont icon-yonghu"
          v-model="loginform.username"
        ></el-input>
      </el-form-item>
      <!-- 密码 -->
      <el-form-item prop="password">
        <el-input
          type="password"
          placeholder="请输入密码"
          prefix-icon="iconfont icon-mima1"
          v-model="loginform.password"
        ></el-input>
      </el-form-item>
      <!-- 按钮 -->
      <el-form-item class="login-bts">
        <el-button type="primary" round @click="logincheck">登录</el-button>
        <el-button type="info" round @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: 'login',
    data(){
      return {
        // 用户信息
        loginform:{
          username:'jintaiyan',
          password:'123'
        },
        // 验证规则
        loginRules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],  
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    methods:{
      logincheck(){
          // 登录前验证
        this.$refs.loginRef.validate(async valid => {
          if(!valid) return;
          // 发送登录请求
          try {
            let {data:res} = await this.$http.post('login', this.loginform)
            this.$message.success('登录成功');
            window.sessionStorage.setItem('token',res.data);
            this.$router.push("/home")
          }
          catch(error) {
            this.$message.error('登录失败')
          }
          // .then(res =>{
          //   this.$message.success('登录成功');
          //   window.sessionStorage.setItem('token',res);
          //   this.$router.push("/home")
          // // console.log(this);
          // })
          // .catch(err =>{
          //   this.$message.error('登录失败')
          // })
          
        })
      },
      // 表单重置
      reset(){
        this.$refs.loginRef.resetFields()
      }
    }
  }
</script>

<style lang="less" scoped>
.login-box {
  width: 400px;
  height: 300px;
  border-radius: 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;

  .login-box-logo {
    width: 130px;
    height: 130px;
    background-color: #fff;
    border-radius: 50%;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login-form {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translate(-50%);
  width: 350px;
}
.login-bts {
  display: flex;
  justify-content: flex-end;
}
</style>